package com.eims.sls_app.ui;

import java.io.Serializable;
import java.util.List;

import com.eims.sls_app.dataaccess.AppObject;
import com.eims.sls_app.dataaccess.DtMatch;
import com.eims.sls_app.dataaccess.DtMatchBJDC;
import com.eims.sls_app.dataaccess.DtMatch_Basketball;
import com.eims.sls_app.dataaccess.Lottery;
import com.eims.sls_app.dataaccess.Schemes;
import com.eims.sls_app.dataaccess.SelectedNumbers;
import com.eims.sls_app.dataaccess.Users;
import com.eims.sls_app.protocol.MD5;
import com.eims.sls_app.utils.App;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.Spanned;
import android.util.Log;

import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Parcelable;

import android.text.Spanned;
import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import progressBar.ProgressDialogBar;

public class MyBaseActivity extends Activity {


    @Override
    public Resources getResources() {
        // TODO Auto-generated method stub
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    @Override
    protected void onResume() {
        super.onResume();

    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        Log.e("xxxa", "tiqu---------");
//		outState.putString("version", App.version);
//		outState.putString("imei", App.imei);
//		outState.putString("type", App.type);
//
		outState.putString("ball", App.ball);
//		outState.putString("followCommissionScale", App.followCommissionScale);
//		outState.putString("followLeastBuyScale", App.followLeastBuyScale);
        outState.putString("lotterysId", App.lotterysId);
//		outState.putString("serverTime", App.serverTime);
//		outState.putString("winToday", App.winToday);
//		outState.putString("isPushKJ", App.isPushKJ);
//		outState.putString("isPushZJ", App.isPushZJ);
//
        outState.putInt("schemeId", App.schemeId);
//		outState.putInt("index", App.index);
//		outState.putInt("bei", App.bei);
//		outState.putInt("qi", App.qi);
//
//		outState.putBoolean("choiceNum", App.choiceNum);
//		outState.putBoolean("isShow", App.isShow);
//		outState.putBoolean("isCanBet", App.isCanBet);
//		outState.putBoolean("isVibrator", App.isVibrator);
//		outState.putBoolean("isSensor", App.isSensor);
//
//		outState.putLong("width", App.width);
//		outState.putLong("height", App.height);
//		outState.putLong("totalCount", App.totalCount);
//		outState.putLong("sum_Income_Money", App.sum_Income_Money);
//
//		outState.putLong("sum_Expense_Money", App.sum_Expense_Money);
//		outState.putLong("sum_Bonus_Money", App.sum_Bonus_Money);
//
        outState.putSerializable("user", App.user);
//		outState.putSerializable("Imagearray", App.Imagearray);
//		outState.putSerializable("list_numbers", (Serializable) App.list_numbers);
//

        outState.putSerializable("scheme", App.scheme);
        outState.putSerializable("lottery", App.lottery);
//		outState.putSerializable("list_Matchs", (Serializable) App.list_Matchs);
//		outState.putSerializable("DtMatch_Basketball", (Serializable) App.DtMatch_Basketball);
//		outState.putSerializable("list_Matchs_bjdc", (Serializable) App.list_Matchs_bjdc);
//		outState.putSerializable("list_singlepass_Matchs", (Serializable) App.list_singlepass_Matchs);
//		outState.putSerializable("DtMatch_Basketball_single", (Serializable) App.DtMatch_Basketball_single);


        super.onSaveInstanceState(outState);
    }


    @SuppressWarnings("unchecked")
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.e("xxxa", "tiqu---------11");
        if (savedInstanceState != null) {

//			App.version = savedInstanceState.getString("version");
//			App.imei = savedInstanceState.getString("imei");
//			App.type = savedInstanceState.getString("type");
//			App.ball = savedInstanceState.getString("ball");
//
//			App.followCommissionScale = savedInstanceState.getString("followCommissionScale");
//			App.followLeastBuyScale = savedInstanceState.getString("followLeastBuyScale");
//            App.lotterysId = savedInstanceState.getString("lotterysId");
//
//			App.serverTime = savedInstanceState.getString("serverTime");
//			App.winToday = savedInstanceState.getString("winToday");
//			App.isPushKJ = savedInstanceState.getString("isPushKJ");
//			App.isPushZJ = savedInstanceState.getString("isPushZJ");
//
            App.schemeId = savedInstanceState.getInt("schemeId");
//			App.index = savedInstanceState.getInt("index");
//			App.bei = savedInstanceState.getInt("bei");
//			App.qi = savedInstanceState.getInt("qi");
//
//			App.choiceNum = savedInstanceState.getBoolean("choiceNum");
//			App.isShow = savedInstanceState.getBoolean("isShow");
//			App.isCanBet = savedInstanceState.getBoolean("isCanBet");
//			App.isVibrator = savedInstanceState.getBoolean("isVibrator");
//			App.isSensor = savedInstanceState.getBoolean("isSensor");
//
//			App.width = savedInstanceState.getLong("width");
//			App.height = savedInstanceState.getLong("height");
//			App.totalCount = savedInstanceState.getLong("totalCount");  
//			App.sum_Income_Money = savedInstanceState.getLong("sum_Income_Money");
//			App.sum_Expense_Money = savedInstanceState.getLong("sum_Expense_Money");
//			App.sum_Bonus_Money = savedInstanceState.getLong("sum_Bonus_Money");
//

            App.scheme = (Schemes) savedInstanceState.getSerializable("scheme");
            App.user = (Users) savedInstanceState.getSerializable("user");
//			App.Imagearray = (String[]) savedInstanceState.getSerializable("Imagearray");
//            App.lottery = (Lottery) savedInstanceState.getSerializable("lottery");
//			App.list_Matchs = (List<List<DtMatch>>) savedInstanceState.getSerializable("list_Matchs");
//			App.DtMatch_Basketball = (List<List<DtMatch_Basketball>>) savedInstanceState
//					.getSerializable("DtMatch_Basketball");
//			App.list_Matchs_bjdc = (List<List<DtMatchBJDC>>) savedInstanceState.getSerializable("list_Matchs_bjdc");
//			App.list_singlepass_Matchs = (List<List<DtMatch>>) savedInstanceState
//					.getSerializable("list_singlepass_Matchs");
//			App.DtMatch_Basketball_single = (List<List<DtMatch_Basketball>>) savedInstanceState
//					.getSerializable("DtMatch_Basketball_single");
//			App.list_numbers = (List<SelectedNumbers>) savedInstanceState.getSerializable("list_numbers");
//			

//			Log.e("xxxa", App.list_Matchs.toString());

        }
    }

    long startTime = 0l;
    long endTime = 0l;

    /**
     * 序列化对象
     *
     * @param -person
     * @return
     * @throws IOException
     */
    private String serialize(Lottery lottery) throws IOException {
        startTime = System.currentTimeMillis();

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                byteArrayOutputStream);
        objectOutputStream.writeObject(lottery);
        String serStr = byteArrayOutputStream.toString("ISO-8859-1");
        serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
        objectOutputStream.close();
        byteArrayOutputStream.close();
//        Log.e("serial", "serialize str =" + serStr);
        endTime = System.currentTimeMillis();
//        Log.e("serial", "序列化耗时为:" + (endTime - startTime));
        return serStr;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        try {
//            ProgressDialogBar.stopProgress();
//        } catch (Exception e) {
//
//        }

    }

    /**
     * 反序列化对象
     *
     * @param str
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private Lottery deSerialization(String str) throws IOException,
            ClassNotFoundException {
        startTime = System.currentTimeMillis();
        String redStr = java.net.URLDecoder.decode(str, "UTF-8");
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                redStr.getBytes("ISO-8859-1"));
        ObjectInputStream objectInputStream = new ObjectInputStream(
                byteArrayInputStream);
        Lottery person = (Lottery) objectInputStream.readObject();
//        Log.e("---", person.toString());
        objectInputStream.close();
        byteArrayInputStream.close();
        endTime = System.currentTimeMillis();
//        Log.e("serial", "反序列化耗时为:" + (endTime - startTime));
        return person;
    }

    void saveObject(String strObject) {
        SharedPreferences sp = getSharedPreferences("Lottery", 0);
        Editor edit = sp.edit();
        edit.putString("Lottery", strObject);
        edit.commit();
    }

    public String getObject() {
        SharedPreferences sp = getSharedPreferences("Lottery", 0);
        return sp.getString("Lottery", null);
    }

    @Override
    protected void onPause() {
        super.onPause();

        try {
            saveObject(serialize(App.lottery));
        } catch (Exception e) {

        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);


        try {
            App.lottery = deSerialization(getObject());



        } catch (Exception e) {

        }


    }


}
